﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Nextropia.C2Manager.C2API
{
    /// <summary>
    /// reprents a C2 system
    /// </summary>
    public class TradingSystem
    {
        /// <summary>
        /// the name of the system on C2
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// the C2 unique identifier of the system
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// the current total equity value of the system
        /// </summary>
        public decimal Equity { get; set; }

        /// <summary>
        /// the current buying power available in the system
        /// </summary>
        public decimal BuyingPower { get; set; }

        /// <summary>
        /// the current cash available for trading in this system
        /// </summary>
        public decimal Cash { get; set; }

        /// <summary>
        /// currently used margin in the system
        /// </summary>
        public decimal MarginUsed { get; set; }

        /// <summary>
        /// list of all open positions in this system
        /// </summary>
        public IList<Position> Positions { private set; get; }

        /// <summary>
        /// list of all signals executed in this system
        /// </summary>
        public IList<Signal> ExecutedSignals { private set; get; }

        /// <summary>
        /// list of all pending (not executed) signals in the system
        /// </summary>
        public IList<Signal> PendingSignals { private set; get; }

        /// <summary>
        /// returns the URL of a C2 system performance chart with variable parameters
        /// </summary>
        /// <param name="startDate">start date of the chart</param>
        /// <param name="endDate">end date of the chart</param>
        /// <param name="width">width of the chart image in pixels</param>
        /// <param name="height">height of the chart image in pixels</param>
        /// <param name="showBars">indicates whether the chart should be a line chart (false) or a bar chart (true)</param>
        public Uri GetChartUri(DateTime startDate, DateTime endDate, int width, int height, bool showBars)
        {
            string baseUrl = "http://www.collective2.com/cgi-perl/livechart.mpl?want={0}&system={1}&width={2}&height={3}&usecache=0".Substitute(showBars ? "risk" : "equity", ID, width, height);
            //string baseUrl = "http://www.collective2.com/cgi-perl/ycharts.mpl?want={0}&system={1}&width={2}&height={3}&usecache=0".Substitute(showBars ? "risk" : "equity", ID, width, height);

            if (startDate != null)
                baseUrl += "&startdate={0:yyyyMMdd}".Substitute(startDate);

            if (endDate != null)
                baseUrl += "&enddate={0:yyyyMMdd}".Substitute(endDate);

            return new Uri(baseUrl);
        }

        /// <summary>
        /// default constructor
        /// </summary>
        public TradingSystem()
        {
            Positions = new List<Position>();
            PendingSignals = new List<Signal>();
            ExecutedSignals = new List<Signal>();
        }

        internal TradingSystem(XmlNode node) : this()
        {
            Name = node.SelectSingleNode("systemname").InnerText;
            ID = node.SelectSingleNode("systemid").InnerText;
        }
    }
}
